﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class GameNetworkProximityChecker : NetworkProximityChecker
{
    private HashSet<NetworkConnection> cachedObservers = new HashSet<NetworkConnection>();
    public bool dontHideAgainIfObserved;
    private RaycastHit[] hits = new RaycastHit[2];

    private bool HasWallBetweenPlayer(PlayerCharacter player)
    {
        Vector3 origin = base.transform.position + ((Vector3) (Vector3.up * 0.5f));
        Vector3 vector2 = player.position + ((Vector3) (Vector3.up * 0.5f));
        Vector3 direction = vector2 - origin;
        int num = Physics.RaycastNonAlloc(origin, direction, this.hits, direction.magnitude, (int) GameLayers.Wall);
        for (int i = 0; i < num; i++)
        {
            RaycastHit hit = this.hits[i];
            if (hit.collider.GetRoot() != base.gameObject)
            {
                return true;
            }
        }
        return false;
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnRebuildObservers(HashSet<NetworkConnection> observers, bool initial)
    {
        if (base.forceHidden)
        {
            NetworkIdentity component = base.GetComponent<NetworkIdentity>();
            if (component.connectionToClient != null)
            {
                observers.Add(component.connectionToClient);
            }
            return true;
        }
        List<PlayerCharacter> all = PlayerCharacter.all;
        for (int i = 0; i < all.Count; i++)
        {
            PlayerCharacter player = all[i];
            if (this.dontHideAgainIfObserved && this.cachedObservers.Contains(player.connectionToClient))
            {
                observers.Add(player.connectionToClient);
            }
            else if (((Vector3.Distance(player.position, base.transform.position) <= base.visRange) && !this.HasWallBetweenPlayer(player)) && ((player.connectionToClient != null) && player.connectionToClient.isReady))
            {
                observers.Add(player.connectionToClient);
                this.cachedObservers.Add(player.connectionToClient);
            }
        }
        return true;
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    public override void OnSetLocalVisibility(bool vis)
    {
        Model component = base.GetComponent<Model>();
        if ((component != null) && (component.instance != null))
        {
            foreach (Renderer renderer in component.instance.GetComponentsInChildren<Renderer>(true))
            {
                renderer.enabled = vis;
            }
        }
        else
        {
            foreach (Renderer renderer2 in base.GetComponentsInChildren<Renderer>(true))
            {
                renderer2.enabled = vis;
            }
        }
    }

    public override void OnStartServer()
    {
        base.OnStartServer();
        base.visRange = 10;
        base.visUpdateInterval = 0.2f;
    }

    private void UNetVersion()
    {
    }
}

